package com.lc.rabbitmq.consumer;

import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author liuc
 * @date 2021/8/16
 * @description 订单的消费
 **/
@Component
public class OrderConsumer {

    //订单状态
    private Map<String, Boolean> orderStatus = new HashMap<>();

    public void addOrder(String orderId){
        //保存订单的状态, true标识正常
        System.out.println("订单:["+orderId+"]待支付!");
        orderStatus.put(orderId, true);
    }

    public void pay(String orderId){
        //订单支付,移除记录
        System.out.println("订单:["+orderId+"]已支付, 移除记录");
        orderStatus.remove(orderId);
    }

    public void expire(String orderId){
        if(orderStatus.get(orderId) != null){
            System.out.println("订单["+ orderId+"]支付过期!");
            //设置过期
            orderStatus.put(orderId,false);
        }else{
            System.out.println("订单["+ orderId+"]已支付!");
        }
    }

    public boolean available(String orderId){
        //判断是否仍然有效
        return orderStatus.get(orderId) != null && orderStatus.get(orderId);
    }

}
